<?php

/**
 * Category form.
 *
 * @package    saf
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class sfInputAssets extends sfWidgetFormInput
{

    protected function configure($options = array(), $attributes = array())
    {
        $this->addOption('thumbs', sfAssetToolkit::_get_thumbs_dims() );
        //var_dump(sfAssetToolkit::_get_thumbs_dims());exit;
        $this->addOption('asset_options', array());
        $this->addOption('with_delete', false);

        //$this->setOption('is_hidden', false);
    }
    public function render($name, $value = null, $attributes = array(), $errors = array())
    {
        sfContext::getInstance()->getConfiguration()->loadHelpers( 'sfAsset' , 'Partial' );
        init_asset_library();

        $options = $this->getOption('asset_options');

        //return input_sf_asset_tag($name, $value,$options);
        return $this->asset_pop($name, $value , $attributes);
    }




    protected function asset_pop($name , $value , $attributes){

        $form_name = 'this.previousSibling.previousSibling.form.name';

        $input_id = $this->generateId($name);


        $html  = input_tag($name."_dummy", $value,array('disabled'=>'disabled')) . '&nbsp;';
        $html .= input_hidden_tag($name, $value,array('id'=>$input_id)) . '&nbsp;';
        $html .= image_tag('/sfAssetsLibraryPlugin/images/folder_open', array(
        'alt' => __('Insert Image'),
        'style' => 'cursor: pointer; vertical-align: middle',
        'onclick' => "
      initialDir = document.getElementById('".$input_id."').value.replace(/\/[^\/]*$/, '');
      if(!initialDir) initialDir = '".sfConfig::get('app_sfAssetsLibrary_upload_dir', 'media')."';
      sfAssetsLibrary.openWindow({
        form_name: ".$form_name.",
        field_name: '".$name."',
        type: 'all',
        url: '".url_for('sfAsset/list?dir=PLACEHOLDER')."?popup=2'.replace('PLACEHOLDER', initialDir),
        scrollbars: 'yes'
      });"
      ));
      if($this->getOption('with_delete')){
          $html .= $this->render_edit_delete( $input_id );
      }

      $html .= "<div class='thumb-div-container asset-id-{$value}' >";
      $html .= get_partial('sfAsset/thumbs/form_thumbs',array('asset_id'=>$value,'thumbs_templates'=>$this->getOption('thumbs')));
      $html .= "</div>";

      return $html;
    }


    protected function render_edit_delete( $id ){
        return link_to( 'Delete asset ' , 'sfAsset/addAsset' ,array('onclick'=>'return delete_asset_image(this);','class'=>'delete') ) ;
    }




}
